﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Ams.Mvc.Bootstrap
{
    public static class BootstrapTextareaExtensions
    {
        #region TextAreaFor
        public static MvcHtmlString TextAreaFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
        {
            return TextAreaFor(helper, expression, null);
        }
        public static MvcHtmlString TextAreaFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
        {
            return TextAreaFor(helper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString TextAreaFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
        {
            htmlAttributes = htmlAttributes ?? new Dictionary<string, object>();
            HtmlMod.AppendToAttributeList(ref htmlAttributes, "class", "form-control");
            var html = TextAreaExtensions.TextAreaFor(helper.Html, expression, htmlAttributes);

            if (helper.Form != null && helper.Form.FormType == Form.Horizontal)
            {
                var divWrapper = new TagBuilder("div");
                divWrapper.AddCssClass(helper.Form.EditorClasses);
                divWrapper.InnerHtml = html.ToString();
                html = divWrapper.ToMvcHtmlString();
            }

            return html;
        }
        public static MvcHtmlString TextAreaFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, int rows, int columns, object htmlAttributes)
        {
            return TextAreaFor(helper, expression, rows, columns, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString TextAreaFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, int rows, int columns, IDictionary<string, object> htmlAttributes)
        {
            htmlAttributes = htmlAttributes ?? new Dictionary<string, object>();
            HtmlMod.AppendToAttributeList(ref htmlAttributes, "class", "form-control");
            HtmlMod.AppendToAttributeList(ref htmlAttributes, "rows", rows.ToString());
            HtmlMod.AppendToAttributeList(ref htmlAttributes, "cols", columns.ToString());
            var html = TextAreaExtensions.TextAreaFor(helper.Html, expression, htmlAttributes);

            if (helper.Form != null && helper.Form.FormType == Form.Horizontal)
            {
                var divWrapper = new TagBuilder("div");
                divWrapper.AddCssClass(helper.Form.EditorClasses);
                divWrapper.InnerHtml = html.ToString();
                html = divWrapper.ToMvcHtmlString();
            }

            return html;
        } 
        #endregion

        #region RichTextFor
        public static MvcHtmlString RichTextFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
        {
            return RichTextFor(helper, expression, null);
        }
        public static MvcHtmlString RichTextFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
        {
            return RichTextFor(helper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString RichTextFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
        {
            htmlAttributes = htmlAttributes ?? new Dictionary<string, object>();
            HtmlMod.AppendToAttributeList(ref htmlAttributes, "class", "form-control input-html");
            var html = TextAreaExtensions.TextAreaFor(helper.Html, expression, htmlAttributes);

            if (helper.Form != null && helper.Form.FormType == Form.Horizontal)
            {
                var divWrapper = new TagBuilder("div");
                divWrapper.AddCssClass(helper.Form.EditorClasses);
                divWrapper.InnerHtml = html.ToString();
                html = divWrapper.ToMvcHtmlString();
            }

            return html;
        }
        public static MvcHtmlString RichTextFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, int rows, int columns, object htmlAttributes)
        {
            return RichTextFor(helper, expression, rows, columns, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString RichTextFor<TModel, TProperty>(this BootstrapHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, int rows, int columns, IDictionary<string, object> htmlAttributes)
        {
            htmlAttributes = htmlAttributes ?? new Dictionary<string, object>();
            HtmlMod.AppendToAttributeList(ref htmlAttributes, "class", "form-control input-html");
            HtmlMod.AppendToAttributeList(ref htmlAttributes, "rows", rows.ToString());
            HtmlMod.AppendToAttributeList(ref htmlAttributes, "cols", columns.ToString());
            var html = TextAreaExtensions.TextAreaFor(helper.Html, expression, htmlAttributes);

            if (helper.Form != null && helper.Form.FormType == Form.Horizontal)
            {
                var divWrapper = new TagBuilder("div");
                divWrapper.AddCssClass(helper.Form.EditorClasses);
                divWrapper.InnerHtml = html.ToString();
                html = divWrapper.ToMvcHtmlString();
            }

            return html;
        }
        #endregion
    }
}
